package com.dones.junit;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.tutorial.domain.DBUser;
import org.hibernate.tutorial.domain.Event;
import org.hibernate.tutorial.domain.Person;
import org.hibernate.tutorial.util.HibernateUtil;
import org.junit.Test;

import com.dones.hibernate.mkyong.Stock;
import com.dones.hibernate.mkyong.StockDailyRecord;
import com.dones.hibernate.mkyong.StockDetail;

public class JunitTest {

	@Test
	public void createAndStoreEvent() {

		createAndStoreEvent("My Event", new Date());
		HibernateUtil.getSessionFactory().close();

	}

	@Test
	public void createAndStorePerson() {

		createAndStorePerson("Manuele", "Dones", 27);
		HibernateUtil.getSessionFactory().close();

	}

	@Test
	public void addPersonToEvent() {
		addPersonToEvent(Long.parseLong("22"), Long.parseLong("23"));
		HibernateUtil.getSessionFactory().close();
	}
	
	@Test
	public void createAndStoreEventWithAnnotation() {
		createAndStoreEventWithAnnotation("My Event", new Date());
		HibernateUtil.getSessionFactory().close();
	}
	
	@Test
	public void createAndStoreOneToOne() {
		createAndStoreOneToOne("7052", "PADINI");
		HibernateUtil.getSessionFactory().close();
	}
	
	@Test
	public void createAndStoreOneToMany() {
		createAndStoreOneToMany("7053", "GOAL");
		HibernateUtil.getSessionFactory().close();
	}
	

	public void createAndStoreEvent(String title, Date theDate) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Event theEvent = new Event();
		theEvent.setTitle(title);
		theEvent.setDate(theDate);
		session.save(theEvent);

		session.getTransaction().commit();
	}

	public void createAndStorePerson(String firstname, String lastname, int age) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Person thePerson = new Person();
		thePerson.setFirstname(firstname);
		thePerson.setLastname(lastname);
		thePerson.setAge(age);
		session.save(thePerson);

		session.getTransaction().commit();
	}

	private void addPersonToEvent(Long personId, Long eventId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Person aPerson = (Person) session.load(Person.class, personId);
		Event anEvent = (Event) session.load(Event.class, eventId);
		aPerson.getEvents().add(anEvent);

		session.getTransaction().commit();
	}
	

	public void createAndStoreEventWithAnnotation(String title, Date theDate) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		DBUser thedbuser = new DBUser();
		thedbuser.setUsername("manuele");
		thedbuser.setCreatedBy("manueledones");
		thedbuser.setCreatedDate(theDate);
		session.save(thedbuser);

		session.getTransaction().commit();
	}
	
	public void createAndStoreOneToOne(String a, String b){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Stock stock = new Stock();
		 
		stock.setStockCode(a);
		stock.setStockName(b);
 
		StockDetail stockDetail = new StockDetail();
		stockDetail.setCompName("PADINI Holding Malaysia");
		stockDetail.setCompDesc("one stop shopping");
		stockDetail.setRemark("vinci vinci");
		stockDetail.setListedDate(new Date());
 
		stock.setStockDetail(stockDetail);
		stockDetail.setStock(stock);
 
		session.save(stock);
		session.getTransaction().commit();
 
		System.out.println("Done");
		
	}
	
	public void createAndStoreOneToMany(String a, String b){
		System.out.println("Hibernate one to many (Annotation)");
		Session session = HibernateUtil.getSessionFactory().openSession();
	 
		session.beginTransaction();
	 
		Stock stock = new Stock();
	        stock.setStockCode(a);
	        stock.setStockName(b);
	        session.save(stock);
	 
	        StockDailyRecord stockDailyRecords = new StockDailyRecord();
	        stockDailyRecords.setPriceOpen(new Float("1.2"));
	        stockDailyRecords.setPriceClose(new Float("1.1"));
	        stockDailyRecords.setPriceChange(new Float("10.0"));
	        stockDailyRecords.setVolume(3000000L);
	        stockDailyRecords.setDate(new Date());
	 
	        stockDailyRecords.setStock(stock);        
	        stock.getStockDailyRecords().add(stockDailyRecords);
	 
	        session.save(stockDailyRecords);
	 
		session.getTransaction().commit();
		System.out.println("Done");
	}


}
